<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;" onclick="parent.xadmin.add_tab('管理员节点列表','/admin/menu/index')">菜单&权限列表</a>
          <a><cite>编辑菜单&权限</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">父级分类</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="pid" id="pid-select" lay-verify="required" lay-filter="pid-select" lay-search>
                        <option value="0" data-level="1">顶级菜单</option>
                        <option th:each="row:${list}" th:value="${row.id}" th:text="${row.title}" th:selected="${data.pid==row.id?'true':'false'}">顶级菜单</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">菜单名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id" th:value="${data.id}">
                    <input type="text" name="title" th:value="${data.title}" lay-verify="required" placeholder="菜单名称"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <input type="text" name="prem" th:value="${data.prem}" lay-verify="required" placeholder="权限名称"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input type="text" style="width: 50%;display:inline-block" name="icon" th:value="${data.icon}" placeholder="请输入节点图标如（layui-icon layui-icon-home 或者fa fa-home）" autocomplete="off" class="layui-input ">
                    <a onclick="parent.parent.xadmin.add_tab('fontawesome字体图标','http://fontawesome.dashgame.com/')" class="layui-btn layui-btn-normal layui-btn-radius" style="margin-left: 5px">fontawesome字体图标</a>
                    <a onclick="parent.parent.xadmin.add_tab('LayUI字体图标','https://www.layui.com/doc/element/icon.html')" class="layui-btn layui-btn-normal layui-btn-radius" href="javascript:;" style="margin-left: 5px">layui图标</a>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="text" name="sorts" th:value="${data.sorts}" lay-verify="number|required" placeholder="分类排序" autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="status" title="显示" value="1" th:checked="${data.status?'true':'false'}"/>
                    <input type="radio" name="status" title="隐藏" value="0" th:checked="${data.status?'false':'true'}"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单</label>
                <div class="layui-input-inline">
                    <input type="radio" name="type" title="是" value="1" th:checked="${data.type?'true':'false'}"/>
                    <input type="radio" name="type" title="否" value="0" th:checked="${data.type?'false':'true'}"/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    layui.use(['jquery', 'form', 'layer','element'], function () {
        var admin = layui.admin,
            $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        layui.element.render();
        //监听提交
        form.on('submit(add)', function (data) {
            let send_json = data.field;
            let url='/admin/menu/edit';

            //发异步，把数据提交给php
            $.ajax({
                url: url,
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("编辑成功", {
                            icon: 1
                        }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });
            return false;
        });

        //遍历select option
        $(document).ready(function () {
            $("#pid-select option").each(function (text) {
                var level = $(this).attr('data-level');
                var text = $(this).text();
                if (level > 1) {
                    text = "├" + text;
                    for (var i = 1; i <= level; i++) {
                        text = "　　" + text;　//js中连续显示多个空格，需要使用全角的空格
                        //console.log(i+"text:"+text);
                    }
                }
                $(this).text(text);
            });
            form.render('select'); //刷新select选择框渲染
        });
    });
</script>
</html>